/**
 * 定时任务管理初始化
 */
var ScheduleJobMgr = {
    job_id: "ScheduleJobMgrTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ScheduleJobMgr.initColumn = function () {
    return [
        {field: 'selectItem', checkbox : true},
        // {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        { title: '任务ID', field: 'job_id',align: 'center', valign: 'middle',sortable: true},
        { title: 'bean名称', field: 'bean_name', align: 'center', valign: 'middle' ,sortable: true},
        { title: '方法名称', field: 'method_name', align: 'center', valign: 'middle' ,sortable: true},
        { title: '参数', field: 'params', align: 'center', valign: 'middle' ,sortable: true},
        { title: 'cron表达式 ', field: 'cron_expression', align: 'center', valign: 'middle' ,sortable: true},
        { title: '备注 ', field: 'remark', align: 'center', valign: 'middle' ,sortable: true},
        { title: '状态', field: 'status', align: 'center', valign: 'middle',
            formatter: function(value, options, row){
            return value === 0 ?
                '<span class="label label-success">正常</span>' :
                '<span class="label label-danger">暂停</span>';
        }},
        {title: '创建时间', field: 'create_time', align: 'center', valign: 'middle',sortable: true}
    ];
};

/**
 * 检查是否选中
 */
ScheduleJobMgr.check = function () {
    var selected = $('#' + this.job_id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else if (selected.length > 1){
        Feng.info("选中表格中的记录大于1条，请先选中表格中的某一记录！");
        return false;
    }else{
        ScheduleJobMgr.seItem = selected[0];
        return true;
    }
};


/**
 * 批量检查是否选中
 */
ScheduleJobMgr.batchCheck = function () {
    var selected = $('#' + this.job_id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        var ids = new Array();
        //遍历所有选择的行数据，取每条数据对应的ID
        $.each(selected, function(i, row) {
            ids[i] = row['job_id'];
            // console.log(ids[i]);
        });
        //定义ajax请求参数
        var param = {
            "ids" : ids
        };
        if(selected.length > 1){
            ScheduleJobMgr.seItem = param;//设置被选中数据的id数组
            // ScheduleJobMgr.seItem = selected;//设置被选中详细数据
        }
        if (selected.length == 1){
            ScheduleJobMgr.seItem = selected[0];
        }
        return true;
    }
};


/**
 * 点击添加定时任务
 */
ScheduleJobMgr.openAddScheduleJobMgr = function () {
    var index = layer.open({
        type: 2,
        title: '添加定时任务',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/scheduleJobMgr/scheduleJobMgr_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看定时任务详情
 */
ScheduleJobMgr.openScheduleJobMgrDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '定时任务详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/scheduleJobMgr/scheduleJobMgr_update/' + ScheduleJobMgr.seItem.job_id
        });
        console.log('1111');
        this.layerIndex = index;
    }
};

/**
 * 删除定时任务
 */
ScheduleJobMgr.delete = function () {
    if (this.batchCheck()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/scheduleJobMgr/delete", function (data) {
                Feng.success("删除成功!");
                ScheduleJobMgr.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
                ajax.set("ids",[ScheduleJobMgr.seItem.job_id]);
            }else {
                Feng.confirm("确认要删除选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条数据吗?",operation);
                ajax.set("ids",ScheduleJobMgr.seItem.ids);
            }
            ajax.start();
        }
        if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
            Feng.confirm("是否删除定时任务:"+ScheduleJobMgr.seItem.method_name + "?",operation);
        }else {
            Feng.confirm("确认要删除选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条数据吗?",operation);
        }
    }
};
/**
 * @Author: Lzx
 * @Description: 暂停定时任务
 * @Params:  * @param null
 * @Date: 23:17 2017/8/27
 */
ScheduleJobMgr.pause = function () {
    if (this.batchCheck()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/scheduleJobMgr/pause", function (data) {
                Feng.success("暂停成功!");
                ScheduleJobMgr.table.refresh();
            }, function (data) {
                Feng.error("暂停失败!" + data.responseJSON.message + "!");
            });
            if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
                ajax.set("jobIds",[ScheduleJobMgr.seItem.job_id]);
            }else {
                Feng.confirm("确认要暂停选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条任务吗?",operation);
                ajax.set("jobIds",ScheduleJobMgr.seItem.ids);
            }
            ajax.start();
        }
        if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
            Feng.confirm("是否暂停定时任务:"+ScheduleJobMgr.seItem.method_name + "?",operation);
        }else {
            Feng.confirm("确认要暂停选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条任务吗?",operation);
        }
    }
};

/**
 * @Author: Lzx
 * @Description: 恢复定时任务
 * @Params:  * @param null
 * @Date: 23:20 2017/8/27
 */
ScheduleJobMgr.resume = function () {
    if (this.batchCheck()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/scheduleJobMgr/resume", function (data) {
                Feng.success("恢复成功!");
                ScheduleJobMgr.table.refresh();
            }, function (data) {
                Feng.error("恢复失败!" + data.responseJSON.message + "!");
            });
            if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
                ajax.set("jobIds",[ScheduleJobMgr.seItem.job_id]);
            }else {
                Feng.confirm("确认要恢复选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条任务吗?",operation);
                ajax.set("jobIds",ScheduleJobMgr.seItem.ids);
            }
            ajax.start();
        }
        if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
            Feng.confirm("是否恢复定时任务:"+ScheduleJobMgr.seItem.method_name + "?",operation);
        }else {
            Feng.confirm("确认要恢复选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条任务吗?",operation);
        }
    }
};

/**
 * @Author: Lzx
 * @Description: 立即执行任务
 * @Params:  * @param null
 * @Date: 23:22 2017/8/27
 */
ScheduleJobMgr.run = function () {
    if (this.batchCheck()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/scheduleJobMgr/run", function (data) {
                Feng.success("立即执行成功!");
                ScheduleJobMgr.table.refresh();
            }, function (data) {
                Feng.error("立即执行失败!" + data.responseJSON.message + "!");
            });
            if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
                ajax.set("jobIds",[ScheduleJobMgr.seItem.job_id]);
            }else {
                Feng.confirm("确认要立即执行选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条任务吗?",operation);
                ajax.set("jobIds",ScheduleJobMgr.seItem.ids);
            }
            ajax.start();
        }
        if (typeof (ScheduleJobMgr.seItem.ids) == "undefined"){
            Feng.confirm("是否立即执行定时任务:"+ScheduleJobMgr.seItem.method_name + "?",operation);
        }else {
            Feng.confirm("确认要立即执行选中的:'"+ScheduleJobMgr.seItem.ids.length + "'条任务吗?",operation);
        }
    }
};

/**
 * 查询定时任务列表
 */
ScheduleJobMgr.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    ScheduleJobMgr.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = ScheduleJobMgr.initColumn();
    var table = new BSTable(ScheduleJobMgr.job_id, "/scheduleJobMgr/list", defaultColunms);
    table.setPaginationType("client");
    ScheduleJobMgr.table = table.init();
});
